﻿/*********************************************
 创建者：  george
 创建时间：2016-10-24
 功能说明：系统模块的增删改查
 ********************************************/
using com.nm.ApiServer;
using com.nm.ApiServer.Attributes;
using com.nm.Attributes;
using com.nm.db.Models.Systems;
using com.nm.Helper;
using EntityFramework.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaseAPI
{
    //[MustAdmin]
    [Description("系统模块的增删改查")]
    public class ModuleHandler: BaseHandler
    {
        [Description("获取模块信息")]
        [ReturnType(typeof(IList<model.ParentModuleData>))]
        public ApiResult GetList()
        {
            //暂时没有关联用户权限
            var dc = new SysEntities();
            var first = GetChildren(0);
            //var result = query.Select(r => new model.ParentModuleData()
            //{
            //    ID = r.ID,
            //    ModuleName = r.ModuleName,
            //    SuperiorModule = r.SuperiorModule,
            //    Grade = r.Grade,
            //    Type = r.Type,
            //    Childrens = GetChildren(r.ID),
            //});
            return Success(first);
        }

        /// <summary>
        /// 获取子菜单
        /// </summary>
        /// <param name="parnetID"></param>
        /// <returns></returns>
        public List<model.ParentModuleData> GetChildren(int parnetID)
        {
            var dc = new SysEntities();
            var query = from c in dc.sys_module
                        where c.SuperiorModule == parnetID && c.IsEffective && c.IsRemove == false
                        orderby c.Sort
                        select new
                        {
                            c.ID,
                            c.ModuleName,
                            c.SuperiorModule,
                            c.Grade,
                            c.Type,
                            c.Sort,
                        };
            return (from c in query select c).ToList().OrderBy(t => t.Sort).Select(t => new model.ParentModuleData
            {
                Grade = t.Grade,
                ID = t.ID,
                ModuleName = t.ModuleName,
                SuperiorModule = t.SuperiorModule,
                Type = t.Type,
                Childrens = GetChildren(t.ID)
            }).ToList();
        }

        [Description("获取指定模块下的页面列表")]
        [ReturnType(typeof(IList<model.ModifyModuleData>))]
        public ApiResult GetPageList([ParamDescription("父级ID")]int ParentID)
        {
            var dc = new SysEntities();
            var query = dc.sys_module.Where(t=>(t.SuperiorModule==ParentID || t.ID==ParentID) && t.IsRemove==false);
            if (!query.Any()) return Success(new model.ModifyModuleData());
            var result = query.OrderBy(t => t.Sort).Select(o => new model.ModifyModuleData
            {
                ID = o.ID,
                IsEffective = o.IsEffective,
                Memo = o.Memo,
                ModuleCode = o.ModuleCode,
                ModuleName = o.ModuleName,
                Sort = (int)o.Sort,
                SuperiorModule = o.SuperiorModule,
                Type = o.Type,
                Url = o.Url
            });
            return Success(result);
        }

        [Description("获取详情")]
        [ReturnType(typeof(model.ModifyModuleData))]
        public ApiResult GetDetails([ParamDescription("模块ID")]int ID, [ParamDescription("级数")]int Grade=2)
        {
            var dc = new SysEntities();
            var query = dc.sys_module.FirstOrDefault(t => t.ID == ID && t.IsRemove == false );
            if (query==null) return Success(new model.ModifyModuleData());
            var result = new model.ModifyModuleData
            {
                ID = query.ID,
                IsEffective = query.IsEffective,
                Memo = query.Memo,
                ModuleCode = query.ModuleCode,
                ModuleName = query.ModuleName,
                Sort = (int)query.Sort,
                SuperiorModule = query.SuperiorModule,
                Type = query.Type,
                Url = query.Url
            };
            return Success(result);
        }

        [Description("新建模块/页面")]
        [ReturnType(typeof(int),"0－成功")]
        [JsonType]
        public ApiResult Create([ParamDescription("输入参数")]model.CreateModuleData param)
        {
            if (param == null) return Error("参数对象为空");
            if (string.IsNullOrWhiteSpace(param.ModuleName)) return Error("模块名称不能为空");
            if (string.IsNullOrWhiteSpace(param.ModuleCode)) return Error("模块编码不能为空");
            if (!param.Type.IsBetween(0, 3)) return Error("无效的模块类型");
            if(!string.IsNullOrWhiteSpace(param.Memo))
                if (param.Memo.Length > 50) return Error("【说明】长度超出限制");
            if (!string.IsNullOrWhiteSpace(param.Url))
                if (param.Url.Length > 200) return Error("【访问地址】长度超出限制");
            if (param.ModuleName.Length > 50) return Error("【模块名称】长度超出限制");
            if (param.ModuleCode.Length > 20) return Error("【模块编号】长度超出限制");
            var dc = new SysEntities();
            if (dc.sys_module.Any(t => t.ModuleName == param.ModuleName && t.IsRemove==false)) return Error("模块名称已存在");
            var module = new sys_module
            {
                ModuleName = param.ModuleName,
                ModuleCode = param.ModuleCode,
                SuperiorModule = param.SuperiorModule,
                IsEffective = param.IsEffective,
                Memo = param.Memo,
                Sort = param.Sort,
                Type = param.Type,
                Url = param.Url,
                IsRemove = false,
                Grade=param.Grade
            };
            dc.sys_module.Add(module);
            try
            {
                dc.SaveChanges();
            }
            catch(Exception ex)
            {
                return Error(ex.Message);
            }
            return Success(0);
        }

        [JsonType]
        [Description("修改模块/页面")]
        [ReturnType(typeof(int), "0－成功")]
        public ApiResult Modify([ParamDescription("输入参数")]model.ModifyModuleData param)
        {
            if (param == null) return Error("参数对象为空");
            if (string.IsNullOrWhiteSpace(param.ModuleName)) return Error("模块名称不能为空");
            if (string.IsNullOrWhiteSpace(param.ModuleCode)) return Error("模块编码不能为空");
            if(param.Type==2)
                if (string.IsNullOrWhiteSpace(param.Url)) return Error("访问地址不能为空");
            if (!string.IsNullOrWhiteSpace(param.Memo))
                if (param.Memo.Length > 50) return Error("【说明】长度超出限制");
            if (!string.IsNullOrWhiteSpace(param.Url))
                if (param.Url.Length > 200) return Error("【访问地址】长度超出限制");
            if (param.ModuleName.Length > 50) return Error("【模块名称】长度超出限制");
            if (param.ModuleCode.Length > 20) return Error("【模块编号】长度超出限制");
            if (!param.Type.IsBetween(0, 3)) return Error("无效的模块类型");
            if (param.ID <= 0) return Error("无效的ID");
            var dc = new SysEntities();
            if (dc.sys_module.Any(t => t.ModuleName == param.ModuleName && t.IsRemove == false && t.ID !=param.ID))
                return Error("模块名称已存在");
            var query = dc.sys_module.FirstOrDefault(t=>t.ID==param.ID);
            if (query == null) return Error("无数据");
            query.IsEffective = param.IsEffective;
            query.Memo = param.Memo;
            query.ModuleCode = param.ModuleCode;
            query.ModuleName = param.ModuleName;
            query.Sort = param.Sort;
            query.SuperiorModule = param.SuperiorModule;
            query.Type = param.Type;
            query.Url = param.Url;
            query.Grade = param.Grade;
            try
            {
                dc.SaveChanges();
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
            return Success(0);
        }

        [Description("删除模块/页面")]
        [ReturnType(typeof(int), "0－成功")]
        public ApiResult Remove([ParamDescription("模块ID")] int ID)
        {
            var dc = new SysEntities();
            var child = dc.sys_module.FirstOrDefault(t => t.SuperiorModule == ID && t.IsRemove == false);
            if (child != null) return Error("该模块存在子模块，无法删除！");
            var query = dc.sys_module.FirstOrDefault(t => t.ID == ID);
            if (query == null) return Error("无数据");
            //逻辑删除
            query.IsRemove = true;
            try
            {
                dc.SaveChanges();
            }
            catch(Exception ex)
            {
                return Error(ex.Message);
            }
            return Success(0);
        }
    }
}
